package org.komarichin.tags;

import com.google.gdata.data.HtmlTextConstruct;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * User: Mykhaylo Komarichyn
 * Date: 17.02.2010
 * Time: 13:52:47
 */
public class ShortContentTag extends TagSupport {
    private static final long serialVersionUID = 573553827458603438L;
    private static final Logger log = LoggerFactory.getLogger(ShortContentTag.class);

    private String content;
    private String length;

    @Override
    public int doStartTag() throws JspException {
        if (StringUtils.isNotEmpty(content)) {
            try {
                int len = NumberUtils.isNumber(length) ? NumberUtils.toInt(length) : 333;
                HtmlTextConstruct text = new HtmlTextConstruct(content);
                String str = text.getPlainText();
                pageContext.getOut().write(str.length() > len ? str.substring(0, len) + " ..." : str);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        return SKIP_BODY;
    }

    @Override
    public void release() {
        super.release();
        content = length = null;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }
}
